<?php
/**
 * Common.php
 *
 * 系统插件全局处理类
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	Momoger
 * @package 	Momoger_Controller
 * @subpackage	Plugin
 * @author 		Guya
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Zend_Controller_Plugin_Abstract */
//require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * 系统插件全局处理类
 *
 * @category	Momoger
 * @package 	Momoger_Controller
 * @subpackage	Plugin
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class Momoger_Controller_Plugin_Common extends Zend_Controller_Plugin_Abstract {
	/**
	 * when the route start setting route
	 *
	 * @param Zend_Controller_Request_Abstract $request
	 */
	public function routeStartup(Zend_Controller_Request_Abstract $request) {
        //get the front controller instance
		$front = Zend_Controller_Front::getInstance();

		//get the router
        $router = $front->getRouter();

        //get custom router setting
        $routes = $front->getParam('bootstrap')->getOption('route');

        //add custom router to static mode
        if (isset($routes['static']) && count($routes['static'])) {
        	foreach ($routes['static'] as $ruleName => $route) {
        		$router->addRoute($ruleName, new Zend_Controller_Router_Route_Static($ruleName, $route));
        	}
        }

        //add custom router to regex mode
    	if (isset($routes['regex']) && count($routes['regex'])) {
        	foreach ($routes['regex'] as $ruleName => $route) {
        		$router->addRoute($ruleName, new Zend_Controller_Router_Route_Regex($route['key'], $route['mvc'], $route['map']));
        	}
        }
    }

	/**
	 * when the route closed, setting application environment
	 *
	 * @param Zend_Controller_Request_Abstract $request
	 */
    public function routeShutdown(Zend_Controller_Request_Abstract $request) {
        // bootstrap
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');

        //get params
        $params = $bootstrap->getOption('resources');

        //get the current module name.
        $module = $request->getModuleName();
        if (!$module) {
        	$module = $params['frontController']['defaultModule'];
        }

        $moduleParams = $params['view'][$module];

        //init, get, setting view
        $bootstrap->bootstrap('View');
        $view = $bootstrap->getResource('View');
        if (is_array($moduleParams['basePath'])) {
        	foreach ($moduleParams['basePath'] as $bp) {
        		$view->addBasePath($bp);
        	}
        } else {
        	$view->addBasePath($moduleParams['basePath']);
        }

        //init, get, setting layout
        $bootstrap->bootstrap('Layout');
        $layout = $bootstrap->getResource('Layout');
        $layout->setLayoutPath($moduleParams['layoutPath']);
    }
}